package com.sumainfor.wx.tenant.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sumainfor.common.base.ITenantSelect;
import com.sumainfor.common.entity.WxUserEntity;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.wx.tenant.entity.TctenTenant;
import com.sumainfor.wx.tenant.service.TctenTenantService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-02-26 19:00
 */
@RequestMapping("/key/provider")
@RestController
public class TenantProviderController {

    @Autowired
    private ITenantSelect tenantSelect;

    @Autowired
    private TctenTenantService tenantService;

    @PostMapping("/getWxInfoByTenId")
    public JsonResult getWxInfoByTenId(String tenId) {
        if (StringUtils.isBlank(tenId)) {
            return JsonResult.errorForEmpty();
        }
        WxUserEntity entity = tenantSelect.getWxBaseInfoByTenId(tenId);
        if (entity == null) {
            return JsonResult.actionFailure();
        }
        return JsonResult.success(entity);
    }

    /**
     * 更新租客端数据
     *
     * @param params
     * @return
     */
    @PostMapping("/updateWxInfoByTenId")
    public JsonResult updateWxInfoByTenId(@RequestBody Map<String, Object> params) {
        String[] key = {"TenId"};
        if (!FieldCheckUtil.isMapFieldNotNull(params, key)) {
            return JsonResult.errorForEmpty();
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("TenId", MapUtils.getString(params, "TenId"));

        TctenTenant tenant = new TctenTenant();
        tenant.setPactAccountId(MapUtils.getString(params, "PactAccountId"));
        boolean state = tenantService.update(tenant, updateWrapper);

        return state ? JsonResult.actionSuccess() : JsonResult.actionFailure();
    }

}
